/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   StatisticalReportServiceImpl.java for com.pengtu.gsj.service.impl.check.sfgl    </p>
 * 
 * <p>最后修改 : $: 2017年7月28日-上午11:23:21 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.sfgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.sfgl.StatisticalReportService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * StatisticalReportServiceImpl
 * 
 * 2017年7月28日 上午11:23:21
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class StatisticalReportServiceImpl implements StatisticalReportService {
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Override
	public Map<String, Object> getStatisticalReport(String stratTime, String endTime, CheckTable checkTable, String tag, String flag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		//得到所有文件
		list = systemManageService.getAllCheckAttachments(stratTime, endTime, tag);
		//查询得分情况
		double total = 0.0;				//得分
		double percentage = 0.0;		//完善度
		double temp = 2.0;				//总分
		//返回时间
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(stratTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(stratTime)+i;
		}
		//1表示存在文件，0表示不存在文件
		List<String> timeResult = new ArrayList<String>(2);
		for (int i = 0; i < years.length; i++) { 
			for (int j = 0; j < list.size(); j++) { 
				if (DateUtils.convertDatetimeToString(list.get(j).getFileTime()).compareTo(years[i]+"-01")>= 0 
						&& DateUtils.convertDatetimeToString(list.get(j).getFileTime()).compareTo(years[i]+"-12")<= 0) {
					if (timeResult.size() <= i) {
						timeResult.add("1");
					}
					if ("0".equals(timeResult.get(i))) {
						timeResult.set(i, "1");
					}
					break; 
				} else {
					if (timeResult.size() <= i) {
						timeResult.add("0");
					}
				}
			}
		}
		int sum = 0;
		for (int i = 0; i < timeResult.size(); i++) {
			sum += Integer.parseInt(timeResult.get(i));
			if (sum == 2) {
				total += 2.0;
			} 
		}
		//判断完善度
		percentage  = MathsUtils.formatDouble((total/2*100),2);
		map.put("percent", MathsUtils.formatDouble(percentage,2));
						
		//得到所有文件
		list = systemManageService.getAllCheckAttachments(stratTime, endTime, tag);
		map.put("att", list);
		map.put("checkTable", checkTable);
		map.put("years", years);
		map.put("sum", total);
		map.put("timeResult", timeResult);
		checkTableService.updateCheckTable(checkTable, percentage, total, temp, true);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
